package com.ux.util
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * @author winnie
	 */	
	public class TextFieldUtil
	{
		
		private static var maxByte : Number;
		private static var txt : TextField;
		
		
		
		private static var dicOnChange : Dictionary = new Dictionary();
		private static var dicTxtField : Dictionary = new Dictionary();
		private static var dicTxtString : Dictionary = new Dictionary();
		
		/**
		 * 텍스트 필드 바이트 체크 메서드
		 * @param txtfield ( target textField type=input )
		 * @param byteSize ( maximum byte size )
		 */		
		public static function preventTxtByte( txtfield:TextField, byteSize:Number, onChange:Function=null ):void
		{
			if( onChange != null )
			{
				dicOnChange[ txtfield ] = onChange;
			}
			
			maxByte = byteSize;
			dicTxtField[txtfield] = txtfield;
			dicTxtField[txtfield].addEventListener(Event.CHANGE , onTextInput );
		}
		
		
		public static function getTxtByte( _tf:TextField ):Number
		{
			return fromStringToByteArray( _tf.text ).length;
		}
		
		public static function spliceTxtByte( value:String, len:Number ):String
		{
			var byteArray: ByteArray = new ByteArray();
			byteArray.writeMultiByte( value, "euc-kr" );
			byteArray.position = 0;
			var str : String = byteArray.readMultiByte( len+1, "euc-kr" );
			
			str = str.substring( 0, str.length-1 );
			return str;
			
		}
		
		private static function onTextInput( e:Event ):void
		{
			var _tf: TextField				= e.currentTarget as TextField;
			var _maxBytes: Number 			= maxByte;
			
			var _currentBytes: Number		= fromStringToByteArray( _tf.text ).length;
			
			if(  _currentBytes > _maxBytes ){
				_tf.text = spliceTxtByte( _tf.text, 3000 );
			}
			
			if( dicOnChange[ _tf ] )
			{
				dicOnChange[ _tf ]();
			}
		}
		
		private static function fromStringToByteArray( value: String ): ByteArray
		{
			var byteArray: ByteArray = new ByteArray();
			byteArray.writeMultiByte( value, "euc-kr" );
			byteArray.position = 0;
			return byteArray;
		}
		
		
		
		
		/**
		 * len 넘는 글자는 mark 로 표시 
		 */		
		public static function truncateByLength(string:String, len:uint, mark:String="...") : String
		{
			if(string.length > len)
				return string.substring(0, len) + mark;
			else
				return string;
		}
		
		
		/**
		 * textfield에서 textWidth 를 기준으로 truncate 시킴.<br />
		 * 만약 wid = -1 이면 textField 의 wid 를 기준으로 한다.
		 */		
		public static function truncateByWidth(tf:TextField, wid:Number=-1, mark:String="...") : Boolean
		{
			wid = ( wid == -1 ) ? tf.width : wid;
			
			if( tf.textWidth +4 <= wid ) return false;
			
			var originAutoSize : String = tf.autoSize;
			var originString : String = tf.text;
			var str : String = tf.text;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = mark;
			
			var markWid : Number = tf.textWidth;
			wid -= markWid;
			
			var i:int = 0;
			var len : int = str.length;
			tf.text = "";
			while( i<len )
			{
				tf.appendText( str.charAt(i) );
				if( tf.textWidth >= wid )
				{
					tf.autoSize = originAutoSize;
					tf.text = tf.text.substr( 0, tf.text.length-2 ) + mark;
					tf.width = wid;
					return true
				}
				
				++i;
			}
			
			tf.text = originString;
			return false;
		}
		
	}
}